package net.xdclass;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) throws InterruptedException {
     System.setProperty("webdriver.chrome.driver","D:\\chromedriver-win32\\chromedriver-win32\\chromedriver.exe");
     ChromeOptions options =new ChromeOptions();
     //允许所有请求
     options.addArguments("--remote-allow-origins=*");
        WebDriver webdriver=new ChromeDriver(options);
        webdriver.manage().window().maximize();
        //idTest(webdriver);
        //nameTest(webdriver);
        //linked(webdriver);
        //classTest(webdriver);
        //cssTest(webdriver);
        //tagnameTest(webdriver);
        //xpathTest(webdriver);
        // weddriverTest(webdriver);
        webElementTest(webdriver);


    }
    public static void idTest(WebDriver webDriver) throws InterruptedException {
        webDriver.get("https://xdclass.net");
        TimeUnit.SECONDS.sleep(2);
        WebElement element = webDriver.findElement(By.id("rc_select_0"));
        element.sendKeys("javase");
    }
    public static void nameTest(WebDriver webDriver) throws InterruptedException {
        webDriver.get("https://www.baidu.com/");
        TimeUnit.SECONDS.sleep(2);
        WebElement element = webDriver.findElement(By.name("wd"));
        element.sendKeys("小狄课堂");

    }
    public static void  linked(WebDriver webDriver) throws InterruptedException {
        webDriver.get("https://xdclass.net");
        TimeUnit.SECONDS.sleep(2);
      webDriver.findElement(By.xpath("/html/body/div/div[2]/div/a/div/div/buttom/span"));
        WebElement element = webDriver.findElement(By.linkText("课程中心"));
        String test=element.getText();
        System.out.println(test);
        element.click();

    }
    public static void cssTest(WebDriver webDriver) throws InterruptedException {
        webDriver.get("https://xdclass.net");
        TimeUnit.SECONDS.sleep(2);
        List<WebElement> elements=webDriver.findElements(By.cssSelector("div[class='title']"));
        System.out.println("size==="+elements.size());
        for(WebElement element:elements){
            String test=element.getText();
            System.out.println(test);
        }
    }
    public static void tagnameTest(WebDriver webDriver) throws InterruptedException {
        webDriver.get("https://baidu.com");
        TimeUnit.SECONDS.sleep(2);
        List<WebElement> elements=webDriver.findElements(By.tagName("a"));
        for(WebElement element:elements){
            String test=element.getText();
            String herf=element.getAttribute("href");
            System.out.println(test+herf);

        }
    }
    public static void classTest(WebDriver webDriver) throws InterruptedException {
        webDriver.get("https://xdclass.net");
        TimeUnit.SECONDS.sleep(2);
        //关闭弹窗
        webDriver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/a/div/div/button/span")).click();
        List<WebElement> elements=webDriver.findElements(By.className("card-box"));
        System.out.println("size==="+elements.size());
        for(WebElement element:elements){
            String test=element.getText();
            System.out.println(test);
        }
    }
    public static void xpathTest(WebDriver webDriver) throws InterruptedException {
        webDriver.get("https://xdclass.net");
        TimeUnit.SECONDS.sleep(2);
        List<WebElement> elements=webDriver.findElements(By.xpath("//*[@class=\"title\"]"));
        for(WebElement element:elements){
            String test=element.getText();
            System.out.println(test);
        }
    }
    public static void weddriverTest(WebDriver webDriver) throws InterruptedException {
        webDriver.get("https://baidu.com");
        String currentUrl=webDriver.getCurrentUrl();
        System.out.println("currentUrl=="+currentUrl);
        String title=webDriver.getTitle();
        System.out.println("title=="+title);
        String pageSource=webDriver.getPageSource();
        System.out.println("pageSource=="+pageSource);
        //关闭浏览器页面
        webDriver.findElement(By.id("kw")).sendKeys("苹果手机");

    }
    public static void webElementTest(WebDriver webDriver) throws InterruptedException {
        webDriver.get("https://baidu.com");
        TimeUnit.SECONDS.sleep(2);
        webDriver.findElement(By.id("kw")).sendKeys("苹果手机");
        TimeUnit.SECONDS.sleep(2);
        webDriver.findElement(By.id("kw")).clear();
        TimeUnit.SECONDS.sleep(2);
        webDriver.findElement(By.id("kw")).sendKeys("ipad");
        webDriver.findElement(By.id("su")).click();
    }
}